from django import forms


class LoginForm(forms.Form):
    username = forms.CharField(
        required=True,
        label=u"用户名",
        error_messages={'required': u'请输入用户名'},
        widget=forms.TextInput(
            attrs={
                'placeholder': u'账号',
                'rows': 1,
                'class': 'input-text size-L',
            }
        ),
    )
    password = forms.CharField(
        required=True,
        label=u"密码",
        error_messages={'required': u'请输入密码'},
        widget=forms.PasswordInput(
            attrs={
                'placeholder': u"密码",
                'rows': 1,
                'class': 'input-text size-L',
            }
        ),
    )


class RegisterForm(forms.Form):
    # CharField负责验证输入并直接在模板中使用
    username = forms.CharField(
        # 此项为必填项
        required=True,
        # 用于显示这个输入项目】的网页显示标签，就是灰色的提示
        label=u'用户名',
        # 如果通不过验证就会提示的信息
        error_messages={'required': u'请输入用户名'},
        # widget负责渲染网页上HTML，表单的输入元素和提取提交的原始数据
        widget=forms.TextInput(
            # 前端页面在渲染的时候，可以通过key-value提取这几个参数
            attrs={
                # 输入框的占位内容
                'placeholder': u"账号",
                # 输入框只显示一行
                'rows': 1,
                # 该输入框的样式，主要时h-ui的样式
                'class': 'input-text size-L',
            }
        ),
    )
    email = forms.EmailField(
        required=True,
        label=u"邮箱",
        error_messages={'required': u'请输入电子邮箱'},
        widget=forms.TextInput(
            attrs={
                'placeholder': u"此邮箱用于密码找回",
                'rows': 1,
                'class': 'input-text size-L',
            }
        ),
    )
    password = forms.CharField(
        required=True,
        error_messages={'required': u'请输入密码'},
        # passwordInput会让密码显示为*，保证密码不会明文显示
        widget=forms.PasswordInput(
            attrs={
                'placeholder': u'密码',
                'rows': 1,
                'class': 'input-text size-L',
            }
        )
    )
    password2 = forms.CharField(
        required=True,
        error_messages={'required': u'请再次输入密码'},
        label='Confirm',
        widget=forms.PasswordInput(
            attrs={
                'placeholder': u"确认密码",
                'rows': 1,
                'class': 'input-text size-L',
            }
        )
    )

    # 该方法判断两次输入的密码是否相同，如果相同返回True，不同返回False
    def pwd_validate(self, p1, p2):
        return p1 == p2


class ChangepwdForm(forms.Form):
    oldpassword = forms.CharField(
        required=True,
        label=u"原密码",
        error_messages={'required': u'请输入原密码'},
        widget=forms.PasswordInput(
            attrs={
                'placeholder': u"原密码",
                'rows': 1,
                'class': 'input-text size-L',
            }
        ),
    )
    newpassword1 = forms.CharField(
        required=True,
        label=u"新密码",
        error_messages={'required': u'请输入新密码'},
        widget=forms.PasswordInput(
            attrs={
                'placeholder': u"新密码",
                'rows': 1,
                'class': 'input-text size-L',
            }
        ),
    )
    newpassword2 = forms.CharField(
        required=True,
        label=u"确认密码",
        error_messages={'required': u'请再次输入新密码'},
        widget=forms.PasswordInput(
            attrs={
                'placeholder': u"确认密码",
                'rows': 1,
                'class': 'input-text size-L',
            }
        ),
    )

    def clean(self):
        print(self.cleaned_data, "%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
        if not self.is_valid():
            raise forms.ValidationError(u"所有项都为必填项")
        elif self.cleaned_data['newpassword1'] != self.cleaned_data['newpassword2']:
            print("*****************************")
            raise forms.ValidationError(u"两次输入的新密码不一样")
        else:
            cleaned_data = super(ChangepwdForm, self).clean()
        return cleaned_data


class ChangeEmailForm(forms.Form):
    new_email = forms.EmailField(
        required=True,
        label=u"新邮箱地址",
        error_messages={'required': u'请输入新邮箱地址'},
        widget=forms.TextInput(
            attrs={
                'placeholder': u"新邮箱地址",
                'rows': 1,
                'class': 'input-text size-L',
            }
        ),
    )
    new_email2 = forms.EmailField(
        required=True,
        label=u"新邮箱地址",
        error_messages={'required': u'请再次输入新邮箱地址'},
        widget=forms.TextInput(
            attrs={
                'placeholder': u"确认新邮箱地址",
                'rows': 1,
                'class': 'input-text size-L',
            }
        ),
    )

    # 重写了clean方法，用于判断两次输入是否相同，返回值一定要是cleaned_data
    # form类方法的运行顺序是：init、clean、validte、save
    def clean(self):
        print(self.cleaned_data, "%%%%%%%%%%%%%%%%%%")
        if not self.is_valid():
            raise forms.ValidationError(u"所有项都为必填项")
        elif self.cleaned_data['new_email'] != self.cleaned_data['new_email2']:
            print("***********************")
            raise forms.ValidationError(u"两次输入的邮箱地址不一样")
        else:
            cleaned_data = super(ChangeEmailForm, self).clean()
        return cleaned_data

